package com.rzl.expense.quartz.task;


import com.rzl.expense.agent.DataAgentService;
import com.rzl.expense.common.utils.DateUtils;
import com.rzl.expense.expense.domain.RentPayment;
import com.rzl.expense.expense.domain.RentPaymentdetail;
import com.rzl.expense.expense.service.IRentPaymentService;
import com.rzl.expense.expense.service.IRentPaymentdetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 房租费缴费明细
 *
 * @author admin
 * @date 2023/08/09
 */
@Slf4j
@Component("RentPaymentDetailTask")  // 2.开启定时任务
public class RentPaymentDetailTask {
    @Autowired
    private DataAgentService dataAgentService;

    @Autowired
    private IRentPaymentdetailService rentPaymentdetailService;


    /**
     * 集团用户同步
     */
    public void run() {
        log.info("获取集团用户数据..........");
        // 执行sql

        int size;
        // limit起始位置
        int startIndex = 0;
        // limit大小
        int limit = 20000;
        rentPaymentdetailService.deleteRentPaymentdetail();
        do {
            List<RentPaymentdetail> list = (List<RentPaymentdetail>) dataAgentService.getData(RuleSqlAdapter.getRentPaymentDetail(startIndex, limit), RentPaymentdetail.class);
            log.info("获取集团用户数据：{}条", list.size());
            if (!Objects.isNull(list) && list.size() > 0) {
                int insertSize = list.size();
                // limit起始位置
                int insertIndex = 0;

                // limit大小
                do {
                    List<RentPaymentdetail> paymentdetails = list.stream().skip(insertIndex).limit(1000).collect(Collectors.toList());
                    try {
                        if (!Objects.isNull(paymentdetails) && paymentdetails.size() > 0) {
                            rentPaymentdetailService.batchRentPaymentdetail(paymentdetails);
                            insertSize = paymentdetails.size();
                            insertIndex = insertIndex + 1000;
                        } else {
                            insertSize = 0;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                } while (insertSize >= 1000);
            }

            size = list.size();
            startIndex = startIndex + limit;
        } while (size >= limit);

        log.info("数据操作成功");
    }
}
